﻿using System;
using System.Collections.Generic;
using System.Web.UI.DataVisualization.Charting;
using System.Web.UI.HtmlControls;
using NPMonitor.DataModel.Report;
using NPMonitor.Implementation;


namespace NPMonitor.Web.UserControls
{
    public partial class Report : System.Web.UI.UserControl
    {
        #region Page Event

        protected void Page_Load(object sender, EventArgs e)
        {
            phChartArea.Visible = false;
        }

        protected void InitChart()
        {
            SplineChart.ChartAreas[0].AxisX.Interval = 1;
            SplineChart.ChartAreas[0].AxisX.IntervalOffset = 1;
            SplineChart.ChartAreas[0].AxisX.LabelStyle.Angle = -60;
            //SplineChart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
            SplineChart.Width = 900;
            SplineChart.Height = 400;
            SplineChart.Series.Clear();

        }

        protected void AppendDataPoint(Series s, string axisLabel, params object[] yValue)
        {
            DataPoint dp = new DataPoint();

            dp.AxisLabel = axisLabel;

            dp.SetValueY(yValue);

            s.Points.Add(dp);
        }

        protected Series CreateSeries(string legend)
        {
            Series s = new Series(legend);

            //s.Color = System.Drawing.Color.DarkGoldenrod;
            //s.IsValueShownAsLabel = true;
            s.ChartType = ChartType;
            s.BorderWidth = 2;
            s.ChartArea = "chartArea";

            Legend l = SplineChart.Legends.Add(legend);
            l.IsDockedInsideChartArea=true;
            l.Name = legend;
            l.Docking = Docking.Right;
            l.TableStyle = LegendTableStyle.Tall;
            return s;
        }

        protected void CreateChart(List<ReportData> originalDataList, ReportRefrenceData baseLine)
        {
            if (originalDataList.Count > 0)
            {
                InitChart();
                foreach (var datalist in originalDataList)
                {
                    Series s = CreateSeries(datalist.Legend);

                    foreach (var item in datalist.list)
                    {
                        AppendDataPoint(s, item.BeginTime, item.AverageTime);
                        //AppendDataPoint(baseLine, item.BeginTime, baseTime);
                    }

                    SplineChart.Series.Add(s);
                }
                if (baseLine!=null && baseLine.RefrenceTime>0)
                {
                    Series refrence = CreateSeries(baseLine.Legend);
                    refrence.Color = System.Drawing.Color.Red;
                    foreach (var item in originalDataList[0].list)
                    {
                        AppendDataPoint(refrence, item.BeginTime, baseLine.RefrenceTime);
                    }
                    SplineChart.Series.Add(refrence);
                }
            }
        }

        protected void BindTable(List<ReportData> datalist)
        {
            if (datalist != null && datalist.Count > 0)
            {
                HtmlTable table = new HtmlTable();
                table.CellSpacing = 1;
                table.CellPadding = 0;
                table.Border = 0;

                HtmlTableRow firstRow = new HtmlTableRow();

                HtmlTableCell firstCell = new HtmlTableCell { InnerText = "In Second" };
                
                firstRow.Cells.Add(firstCell);

                foreach (var data in datalist)
                {
                    var cell = new HtmlTableCell();
                    cell.InnerText = data.Legend;
                    cell.Attributes.Add("style", "background-color:#0978c0; color:#fff;font-weight:bold;");
                    firstRow.Cells.Add(cell);
                }

                table.Rows.Add(firstRow);

                foreach (var point in datalist[0].list)
                {
                    HtmlTableRow contentRow = new HtmlTableRow();
                    contentRow.Cells.Add(new HtmlTableCell
                    {
                        InnerText = point.BeginTime
                    });
                    foreach (var item in datalist)
                    {
                        foreach (var signalPoint in item.list)
                        {
                            if (signalPoint.BeginTime == point.BeginTime)
                            {
                                contentRow.Cells.Add(new HtmlTableCell
                                {
                                    InnerText = signalPoint.AverageTime != 0 ? string.Format("{0:N3}",signalPoint.AverageTime) : "-"
                                });
                            }
                        }
                    }
                    table.Rows.Add(contentRow);
                }

                phTable.Controls.Add(table);
            }
        }
         
        #endregion

        private SeriesChartType chartType = SeriesChartType.Spline;

        public SeriesChartType ChartType
        {
            get { return chartType; }
            set { chartType = value; }
        }

        public void BulidChart(List<ReportData> datalist, ReportRefrenceData baseLine)
        {
            BindTable(datalist);
            CreateChart(datalist, baseLine);
            phChartArea.Visible = true;
        }

        public bool DisableTable
        {
            set
            {
                phTable.Visible = !value;
            }
        }
    }
}